package jsj.lib.example

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import jsj.open.lib.FileViewer

class MainActivity : AppCompatActivity() {
    protected lateinit var context: Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        context = this
        setContentView(R.layout.activity_main)
        initPermission()
    }

    private fun initPermission() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
            XXPermissions.with(this) // 申请单个权限
//                            .permission(Permission.READ_EXTERNAL_STORAGE)
                // 申请多个权限
                //                .permission(Permission.Group.STORAGE)
//            .permission(Permission.WRITE_EXTERNAL_STORAGE)
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .permission(Permission.CAMERA)
                .permission(Permission.RECORD_AUDIO) // 设置权限请求拦截器（局部设置）
                //.interceptor(new PermissionInterceptor())
                // 设置不触发错误检测机制（局部设置）
                //.unchecked()
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: List<String?>, allGranted: Boolean) {
                        if (!allGranted) {
                            return
                        }
                    }

                    override fun onDenied(permissions: List<String?>, doNotAskAgain: Boolean) {
                        if (doNotAskAgain) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(context, permissions)
                        } else {
                            toast("申请存储、相机扫码等权限失败")
                        }
                    }
                })
        }else{
            XXPermissions.with(this) // 申请单个权限
                // 申请多个权限
                .permission(Permission.READ_MEDIA_IMAGES)
                .permission(Permission.READ_MEDIA_AUDIO)
                .permission(Permission.READ_MEDIA_VIDEO)
//                .permission(Permission.Group.STORAGE)
                .permission(Permission.CAMERA)
                .permission(Permission.RECORD_AUDIO) // 设置权限请求拦截器（局部设置）
                //.interceptor(new PermissionInterceptor())
                // 设置不触发错误检测机制（局部设置）
                //.unchecked()
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: List<String?>, allGranted: Boolean) {
                        if (!allGranted) {
                            toast("获取部分权限成功，但部分权限未正常授予")
                            return
                        }
                        toast("申请存储、相机扫码等权限成功")
                        findViewById<FileViewer>(R.id.file_view).refresh()
                    }

                    override fun onDenied(permissions: List<String?>, doNotAskAgain: Boolean) {
                        if (doNotAskAgain) {
                            toast("被永久拒绝授权，请手动授予存储权限")
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(context, permissions)
                        } else {
                            toast("申请存储、相机扫码等权限失败")
                        }
                    }
                })
        }


    }
    protected fun toast(text: String?) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
    }
}